﻿using Base.FileRemoteModel.UpFile;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Helper;

namespace Shop.ExtendService.lmpl
{
    [ClassLifetimeAttr(ClassLifetimeType.SingleInstance)]
    internal class FileService : IFileService
    {
        public void Save ( long fileId, long linkBizPk, params long[] dropId )
        {
            new SaveFile
            {
                FileId = fileId,
                LinkBizPk = linkBizPk,
                DropFileId = dropId
            }.SyncSend();
        }
        public void Save ( long[] fileId, long linkBizPk, params long[] dropId )
        {
            new SaveFileList
            {
                FileId = fileId,
                LinkBizPk = linkBizPk,
                DropFileId = dropId
            }.SyncSend();
        }
        public void Drop ( string dirKey, long linkBizPk )
        {
            new DropFile
            {
                DirKey = dirKey,
                LinkBizPk = linkBizPk
            }.SyncSend();
        }
        public void Drop ( string dirKey, long linkBizPk, string tag )
        {
            new DropFile
            {
                DirKey = dirKey,
                LinkBizPk = linkBizPk,
                Tag = tag
            }.SyncSend();
        }
        public void Drop ( long[] fileId )
        {
            new BatchDropFileById
            {
                Ids = fileId
            }.SyncSend();
        }
        public void Drop ( long fileId )
        {
            new DropFileById
            {
                FileId = fileId
            }.SyncSend();
        }
        public void Drop ( string fileUri )
        {
            long id = long.Parse(Path.GetFileNameWithoutExtension(fileUri));
            new DropFileById
            {
                FileId = id
            }.SyncSend();
        }
        public void Sync ( long? fileId, long linkBizPk, string fileUri )
        {
            if ( fileId.HasValue == false && fileUri.IsNull() )
            {
                return;
            }
            else if ( fileId.HasValue == false )
            {
                this.Drop(fileUri);
            }
            else if ( fileUri.IsNull() )
            {
                this.Save(fileId.Value, linkBizPk);
            }
            else
            {
                long id = long.Parse(Path.GetFileNameWithoutExtension(fileUri));
                if ( id == fileId.Value )
                {
                    return;
                }
                this.Save(fileId.Value, linkBizPk, id);
            }
        }
        public void Sync ( long[] fileId, long linkBizPk, string[] fileUri )
        {
            if ( fileId.IsNull() && fileUri.IsNull() )
            {
                return;
            }
            else if ( fileUri.IsNull() )
            {
                this.Save(fileId, linkBizPk, null);
            }
            else
            {
                long[] dropId = fileUri.ConvertAll(c => long.Parse(Path.GetFileNameWithoutExtension(c)));
                this.Sync(fileId, linkBizPk, dropId);
            }
        }
        public void Sync ( long[] fileId, long linkBizPk, long[] dropId )
        {
            if ( !fileId.IsNull() && !dropId.IsNull() )
            {
                long[] ids = dropId.Intersect(fileId).ToArray();
                if ( ids.Length > 0 )
                {
                    fileId = fileId.Remove(ids);
                    dropId = dropId.Remove(ids);
                }
            }
            this._Sync(fileId, linkBizPk, dropId);
        }
        private void _Sync ( long[] fileId, long linkBizPk, long[] dropId )
        {
            if ( fileId.IsNull() && dropId.IsNull() )
            {
                return;
            }
            else if ( fileId.IsNull() )
            {
                this.Drop(dropId);
            }
            else
            {
                this.Save(fileId, linkBizPk, dropId);
            }
        }
        public void Drop ( string dirKey, long[] linkBizPk )
        {
            new BatchDropFile
            {
                DirKey = new string[] { dirKey },
                LinkBizPk = linkBizPk
            }.SyncSend();
        }

        public void Drop ( string[] dirKey, long[] linkBizPk )
        {
            new BatchDropFile
            {
                DirKey = dirKey,
                LinkBizPk = linkBizPk
            }.SyncSend();
        }
        public void Drop ( string[] dirKey, long linkBizPk )
        {
            new BatchDropFile
            {
                DirKey = dirKey,
                LinkBizPk = new long[] { linkBizPk }
            }.SyncSend();
        }
    }
}
